# -*- coding: utf-8 -*-
# @Time    : 2018/8/16 17:11
# @Author  : 聂家利
# @Email   : 1073438012@qq.com
# @File    : helper.py
# @Software: PyCharm
import os
import shutil
import collections


def copyfile(source_file, target_file):
    # 分离文件名和路径
    file_path, file_name = os.path.split(target_file)  
    if not os.path.exists(file_path):
        # 创建路径
        os.makedirs(file_path) 
    try:
        print('copyfile '+source_file+' to '+target_file)
        # 复制文件
        shutil.copyfile(source_file, target_file)  
        msg = 1
        data = 'copy'+source_file+'to'+source_file+'success'
    except Exception as e:
        msg = 0
        data = e
    return msg, str(data)


def get_dir_copy_file(source_path, target_path):

    if not os.path.exists(source_path):
        return

    if not os.path.exists(target_path):
        os.makedirs(target_path)
    # 创建一个队列来存放路径
    queue_path = collections.deque()
    # 创建一个队列用来存放目标路径
    queue_target_path = collections.deque()
    # 将路径存进队列
    queue_path.append(source_path)
    # 将目标文件路径存进队列
    queue_target_path.append(target_path)
    while True:

        # 如果原文件路径队列为空，则跳出循环
        if len(queue_path) == 0:
            break
        # 从原文件路径队列中向左取出路径
        path = queue_path.popleft()
        # 从目标路径队列中向左取出路径
        target_path = queue_target_path.popleft()
        data = []
        msg = 1
        # 遍历出path
        for fileName in os.listdir(path):
            # 拼接路径
            abs_path = os.path.join(path, fileName)
            abs_target = os.path.join(target_path, fileName)
            print(abs_path, abs_target)
            # 判断是否是文件，是就进行复制
            if os.path.isfile(abs_path):
                copy_msg, copy_data = copyfile(abs_path, abs_target)
                log = {'msg': copy_msg, 'data': copy_data}
                data.append(log)

            # 如果是路径，则存进队列
            if os.path.isdir(abs_path):
                if os.path.exists(abs_target):
                    os.rmdir(abs_target)
                os.mkdir(abs_target)
                queue_target_path.append(abs_target)
                queue_path.append(abs_path)
    return msg, data


def copy_dir_or_file(source, target):
    if os.path.isdir(source):
        return get_dir_copy_file(source, target)
    elif os.path.isfile(source):
        return copyfile(source, target)
    else:
        msg = 0
        data = source+'is not file or dir'
    return msg, data


# def copyfile(source_file, target_file):
#     print(source_file, target_file)
#     if not os.path.isfile(source_file):
#         result = 0
#     else:
#         file_path, file_name=os.path.split(target_file)    # 分离文件名和路径
#         if not os.path.exists(file_path):
#             os.makedirs(file_path)                 # 创建路径
#         shutil.copyfile(source_file, target_file)      # 复制文件
#         result = 1
#     return result
# def copyFile(sourceDir,  targetDir):
  # try:    if os.path.exists(targetDir) and os.path.isfile(targetDir):
      #         os.chmod(targetDir, stat.S_IWRITE)
      #         os.remove(targetDir)
      #         shutil.copy(sourceDir, targetDir)
      #             else:
      #         shutil.copy(sourceDir,  targetDir)
  #         return True


# def movefile(source_file, target_file):
#     if not os.path.isfile(source_file):
#         print("%s not exist!"%(source_file))
#     else:
#         file_path, file_name = os.path.split(target_file)    # 分离文件名和路径
#         if not os.path.exists(file_path):
#             os.makedirs(file_path)                # v创建路径
#         shutil.move(source_file, target_file)          #移动文件